
(function($, win, doc){
	
	/**
	 *  默认值：/seeyon
	 */
	var appBasePath = (function(){
		
		var path = location.pathname;
		
		return path.substring(0, path.indexOf('/', 1));
	})();
	
	
	var app = new Vue({
		el: '#app',
		data: {
			basic:{
				
			},
			style:{
				
			},
			tableData:[
				{
					id: null,
					name: '...',
					product: '...',
					driver_type: 'JDBC',
					driver_class: '...',
					driver_url: '...',
					username: '...',
					password: '...',
					mark: '...',
					description: '...'
				}
			],
			formData:{
				fields:{
					id: null,
					name: '',
					product: '',
					driver_type: 'JDBC',
					driver_class: '',
					driver_url: '',
					username: '',
					password: '',
					mark: '',
					description: ''
				},
				rules: {
				  name: [
					  { required: true, message: '请输入名称', trigger: 'blur' },
					  { min: 4, max: 20, message: '长度在 4 到 20 个字符', trigger: 'blur' }
				  ],
				  product: [
					  { required: true, message: '请选择数据库类型', trigger: 'change' }
				  ],
				  driver_class: [
					  { required: true, message: '请输入驱动类名', trigger: 'blur' }
				  ],
				  driver_url: [
					  { required: true, message: '请输入数据源URL', trigger: 'blur' }
				  ],
				  username: [
					  { required: true, message: '请输入用户名', trigger: 'blur' }
				  ],
				  password: [
					  { required: true, message: '请输入密码', trigger: 'blur' }
				  ],
				  mark: [
					  { required: true, message: '请输入连接标识', trigger: 'blur' },
					  { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
				  ]
				}
			}
		},
		methods:{
			init: function(){
				this.loadDBList();
			},
			loadDBList: function(){
				
				var _this = this;
				
				$.http.get({
					url: (appBasePath + '/snail/db.do?method=list'),
					header: {'Accept': 'application/json'},
					onsuccess: function(result, status, xhr){
						
						if(200 == result.code){
							
							_this.tableData=result.data;
							
						}else{
							alert(result.message);
						}
					},
					onerror: function(responseText, status, xhr){
						console.log(responseText);
					}
				});
			},
			getFormData: function(){
				
				var data = {
					name: this.formData.fields.name.trim(),
					product: this.formData.fields.product,
					driver_type: this.formData.fields.driver_type,
					driver_class: this.formData.fields.driver_class.trim(),
					driver_url: this.formData.fields.driver_url,
					username: this.formData.fields.username,
					password: this.formData.fields.password,
					mark: this.formData.fields.mark,
					description: this.formData.fields.description
				}
				
				if(this.basic.isUpdate){
					data['id'] = this.formData.fields.id;
				}
				
				return data;
			},
			setFormData: function(data){
				this.formData.fields.id = data.id;
				this.formData.fields.name = data.name;
				this.formData.fields.product = data.product;
				this.formData.fields.driver_type = data.driver_type;
				this.formData.fields.driver_class = data.driver_class;
				this.formData.fields.driver_url = data.driver_url;
				this.formData.fields.username = data.username;
				this.formData.fields.password = data.password;
				this.formData.fields.mark = data.mark;
				this.formData.fields.description = data.description;
			},
			onDBProductChang: function(value){
				
				console.log(value);
				
				var info = {
					'MYSQL':        ['com.mysql.jdbc.Driver', 'jdbc:mysql://[IP]:[PORT|3306]/[DBNAME]?useUnicode=true&characterEncoding=utf8'],
					'MYSQL6X':      ['com.mysql.cj.jdbc.Driver', 'jdbc:mysql://[IP]:[PORT|3306]/[DBNAME]?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false'],
					'ORACLE':       ['oracle.jdbc.driver.OracleDriver', 'jdbc:oracle:thin:@[IP]:[PORT|1521][/SERVICE_NAME|:SID]'],
					'SQLSERVER':    ['com.microsoft.sqlserver.jdbc.SQLServerDriver', 'jdbc:sqlserver://[IP]:[PORT|1433];DatabaseName=[DBNAME]'],
					'POSTGRESQL':   ['org.postgresql.Driver', 'jdbc:postgresql://[IP]:[PORT|5432]/[DBNAME]'],
					'DB2':          ['com.ibm.db2.jcc.DB2Driver', 'jdbc:db2://[IP]:[PORT|5000]/[DBNAME]'],
					'DM':           ['dm.jdbc.driver.DmDriver', 'jdbc:dm://[IP]:[PORT|5236]/[DBNAME]'],
					'KINGBASE':     ['com.kingbase8.Driver', 'jdbc:kingbase8://[IP]:[PORT|54321]/[DBNAME]'],
					'OSCAR':        ['com.oscar.Driver', 'jdbc:oscar://[IP]:[PORT|2003]/[DBNAME]']
				}

				this.formData.fields.driver_class = info[value][0];
				this.formData.fields.driver_url = info[value][1];
			
			},
			onTest: function(){
				
				var _this = this;
				
				this.$refs.formData.validate(function(valid){
					
					if(valid){

						$.http.post({
							url: (appBasePath + '/snail/db.do?method=test'),
							header: {'Content-Type': 'application/json', 'Accept': 'application/json'},
							data: _this.getFormData(),
							onsuccess: function(result, status, xhr){
					
								alert(result.message);
							},
							onerror: function(responseText, status, xhr){
								console.log(responseText);
							}
						});
					}else{
						alert('数据校验不通过！');
					}
				});
			},
			onSave: function(){
				
				var _this = this;
				
				this.$refs.formData.validate(function(valid){
					
					if(valid){

						$.http.post({
							url: (appBasePath + '/snail/db.do?method=save'),
							header: {'Content-Type': 'application/json', 'Accept': 'application/json'},
							data: _this.getFormData(),
							onsuccess: function(result, status, xhr){
								
								if(200 == result.code){
									
									_this.loadDBList();
									
								}else{
									alert(result.message);
								}
							},
							onerror: function(responseText, status, xhr){
								console.log(responseText);
							}
						});
					}else{
						alert('数据校验不通过！');
					}
				});
			},
			onEdit: function(index, row){
				
				this.setFormData(row);
				
			},
			onDelete: function(index, row){
				
				var _this = this;
				
				$.http.post({
					url: (appBasePath + '/snail/db.do?method=delete'),
					header: {'Accept': 'application/json'},
					data: {mark: row.mark},
					onsuccess: function(result, status, xhr){
						
						if(200 == result.code){
							
							_this.loadDBList();
							
						}else{
							alert(result.message);
						}
					},
					onerror: function(responseText, status, xhr){
						console.log(responseText);
					}
				});
			}
		},
		created: function(){
			this.init();
		}
	});
	
	
})(Snail, window, document);